package com.baikeyang.common.util;



import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

/**
 * @className DBGeneratorUtil
 * @description 持久层代码自动生成
 * @author BaiKeyang@vip.qq.com
 * @date 2016年1月27日 上午10:01:52
 *
 */
public class DBGeneratorUtil {
    public static void main(String[] args) {
        try {
//        	String rootPath = System.getProperty("user.dir");
//        	rootPath = rootPath.substring(0,rootPath.lastIndexOf("\\")) + "/";
//        	System.out.println("Project root path :"+rootPath);
//        	System.setProperty("targetRootDir", rootPath);
//        	config(rootPath);
//            System.err.println("start.................");
//            List<String> warnings = new ArrayList<String>();
//            boolean overwrite = true;
//            ConfigurationParser cp = new ConfigurationParser(warnings);
//            Configuration config = cp.parseConfiguration(DBGeneratorUtil.class
//                    .getResourceAsStream("/generator/generatorConfig.xml"));
//            DefaultShellCallback callback = new DefaultShellCallback(overwrite);
//            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
//                    callback, warnings);
//            myBatisGenerator.generate(null);
            System.err.println("end.................");
        } catch (Exception e) {
            e.printStackTrace();
            System.err.print("数据库实例生成失败！");
        }
    }

    public static void config(String rootPath) throws FileNotFoundException, IOException {
    	Properties p = new Properties();
    	p.load(DBGeneratorUtil.class.getClassLoader().getResourceAsStream("generator/generator.properties"));
    	// 项目结构模块名称
    	String pojoModuleName = p.getProperty("pojo.module.name", "framework.common");
    	String mapperModuleName = p.getProperty("mapper.module.name");
    	String tabelName = p.getProperty("table.name");
    	String entityName = p.getProperty("entity.name");
    	if(isEmpty(mapperModuleName) || isEmpty(tabelName) || isEmpty(entityName)){
    		throw new IOException("[mapper.module.name | table.name | entity.name] can not be null");
    	}

    	String pojoPkg_prefix = p.getProperty("pojo.pkg.prefix" , "com.baikeyang.common.model");
    	String mapperPkg_prefix = p.getProperty("mapper.pkg.prefix");
    	String mapperxmlPkg_prefix = p.getProperty("mapperxml.pkg.prefix" , "mapper");

    	// 实体名称首字母小写
    	String modPkg = entityName.toLowerCase();
    	String pojoPkg = pojoPkg_prefix.concat(".").concat(modPkg);
    	String mapperPkg = mapperPkg_prefix.concat(".").concat(modPkg).concat(".mapper");
    	String mapperxmlPkg = mapperxmlPkg_prefix.concat(".").concat(modPkg);

    	String pojoSavePath = rootPath + pojoModuleName.concat("/src/main/java");
    	String mapperSavePath = rootPath + mapperModuleName.concat("/src/main/java");
    	// mapper类与xml放置在同一个项目模块中
    	String mapperxmlSavePath = rootPath + mapperModuleName.concat("/src/main/resources");

    	String s = p.getProperty("pojo.pkg.name");
    	if(isEmpty(s)){
    		System.setProperty("pojo.pkg.name", pojoPkg);
    	}else{
    		pojoPkg = s;
    	}
    	s = p.getProperty("mapper.pkg.name");
    	if(isEmpty(s)){
    		System.setProperty("mapper.pkg.name", mapperPkg);
    	}else{
    		mapperPkg = s;
    	}
    	s = p.getProperty("mapperxml.pkg.name");
    	if(isEmpty(s)){
    		System.setProperty("mapperxml.pkg.name", mapperxmlPkg);
    	}else{
    		mapperxmlPkg = s;
    	}
    	s = p.getProperty("pojo.save.path");
    	if(isEmpty(s)){
    		System.setProperty("pojo.save.path", pojoSavePath);
    	}else{
    		pojoSavePath = s;
    	}
    	s = p.getProperty("mapper.save.path");
    	if(isEmpty(s)){
    		System.setProperty("mapper.save.path", mapperSavePath);
    	}else{
    		mapperSavePath = s;
    	}
    	s = p.getProperty("mapperxml.save.path");
    	if(isEmpty(s)){
    		System.setProperty("mapperxml.save.path", mapperxmlSavePath);
    	}else{
    		mapperxmlSavePath = s;
    	}

    	System.out.println("Pojo 文件生成路径 :" + pojoSavePath + " " + pojoPkg);
    	System.out.println("Mapper 文件生成路径 :" + mapperSavePath + " " + mapperPkg);
    	System.out.println("Mapperxml 文件生成路径 :" + mapperxmlSavePath + " " + mapperxmlPkg);
    }


    public static boolean isEmpty(String name) throws IOException {
    	if(null == name || "".equals(name)){
    		return true;
    	}
    	return false;
    }
}
